package com.controller.ht;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.ajax.AjaxRes;
import com.common.utils.AppMD5Util;
import com.common.utils.base.UuidUtil;
import com.controller.base.BaseController;
import com.entity.AppendixRelationship;
import com.entity.Login;
import com.entity.Product;
import com.service.AppendixRelationshipService;
import com.service.LoginService;

@Controller
public class LoginController extends BaseController<Object>{

	@Autowired
	private LoginService loginservice;
	@Autowired
	private AppendixRelationshipService apprpservice;
	/**
	 * 用户登录
	 * @return
	 */
	@RequestMapping("/login")
	@ResponseBody
	public AjaxRes login(Login login){
		AjaxRes ajax=new AjaxRes();
		List<Login> findlog=loginservice.find(login);
		if(findlog!=null && !findlog.isEmpty()){
			if(findlog.get(0).getLoginPwd().equals(AppMD5Util.MD5(login.getLoginPwd()))){
				HttpSession session=getRequest().getSession();
				session.setAttribute("login", findlog.get(0));
				ajax.setResMsg("登陆成功");
				return ajax;
			}else{
				ajax.setResMsg("账号或密码输入错误1");
				return ajax;
			}
		}else{
			ajax.setResMsg("账号或密码输入错误2");
			return ajax;
		}
	}
	/**
	 * 注销登录
	 * @return
	 */
	@RequestMapping("/loginOut")
	@ResponseBody
	public ModelAndView loginOut(){
		ModelAndView mv =  new ModelAndView();
		System.out.println("注销=======================");
		HttpSession session=getRequest().getSession();
		session.setAttribute("login", null);
		mv.setViewName("pages/login");
		return mv;
	}
	/**
	 * 用户注册
	 * @return
	 */
	@RequestMapping("/register")
	@ResponseBody
	public AjaxRes register(Login login){
		System.out.println("注册=======================");
		AjaxRes ajax=new AjaxRes();
		if(login.getLoginAccount().equals("")&&login.getLoginPwd().equals("")){
			ajax.setResMsg("注册失败");
			return ajax;
		}else{
			String pwd = AppMD5Util.MD5(login.getLoginPwd());
			login.setId(UuidUtil.get32UUID());
			login.setLoginPwd(pwd);
			login.setPwdEncryption("MD5");
			login.setLastLoginTime(new Date());
			login.setLoginIp("未知");
			System.out.println(login.toString());
			Integer a = loginservice.insert(login);
			if(a==0){
				ajax.setResMsg("注册失败");
			}if(a==1){
				ajax.setResMsg("注册成功");
			}
			return ajax;
			
		}
	}
	/**
	 * 修改用户信息并返回修改结果
	 * @return
	 */
	@RequestMapping("/updateLogin")
	@ResponseBody
	public AjaxRes update(Login login,String apprpId,String url){
		System.out.println(apprpId+"=================="+url);
		AjaxRes res=new AjaxRes(); 
		AppendixRelationship apprp = new AppendixRelationship();
		apprp.setRelationNo(login.getId());
		List<AppendixRelationship> apprpUis = apprpservice.find(apprp);
		try {
			String id = apprpUis.get(0).getId();
			System.out.println("有");
			apprpUis.get(0).setRelationTName("login");
			apprpUis.get(0).setUrl(url);
			AppendixRelationship apprpDel = new AppendixRelationship();
			apprpDel.setId(apprpId);
			apprpservice.delete(apprpDel);
			System.out.println(apprpUis.get(0));
			apprpservice.update(apprpUis.get(0));
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("没有");
			apprp.setId(apprpId);
			apprp.setRelationNo("");
			System.out.println(apprp.toString());
			apprpUis = apprpservice.find(apprp);
			System.out.println(apprpUis.get(0));
			apprpUis.get(0).setRelationNo(login.getId());
			apprpUis.get(0).setRelationTName("login");
			apprpUis.get(0).setUrl(url);
			apprpservice.update(apprpUis.get(0));
		}
		HttpSession session=getRequest().getSession();
		Login loginInfo =(Login) session.getAttribute("login");
		loginInfo.setUrlImg(url);
		session.setAttribute("login", loginInfo);
		res.setResMsg("修改成功");
		return res;
	}
}
